<?php
session_start();
include("gestionPseudo.php");
    
function formatTest($pseudo,$mess)
{
	$res='[pseudo]'.$pseudo.'[/pseudo][date]'.date("d/m/Y H:i:s").'[/date][mess]'.$mess.'[/mess]'."\n";	
	return $res;
}

function verifClose($chaine,$nb,$nbF,$balise)
{
	if(	$nb !=	$nbF )
	{
		$nbClose= $nb -$nbF;
		for($i=0;$i!=$nbClose;$i++)
		{
			$chaine=$chaine.$balise;
		}
	}
	return $chaine;
}

function miseEnForme($chaine)
{

	// remplace < and > to fight against bad joke
	$chaine = str_replace("<", " ", $chaine);
	$chaine = str_replace(">", " ", $chaine);
	$chaine = str_replace("<", " ", $chaine);
	
	// Blod
	$nbBlod=0;
	$nbFBlod=0;
	$chaine = str_replace("[b]", "<strong>", $chaine,$nbBlod);
	$chaine = str_replace("[/b]","</strong>", $chaine,$nbFBlod); 
	
	// Italic
	$nbI=0;
	$nbFI=0;
	$chaine = str_replace("[i]", "<i>", $chaine, $nbI);
	$chaine = str_replace("[/i]","</i>", $chaine, $nbFI); 
	
	//underline
	$nbU=0;
	$nbFU=0;
	$chaine = str_replace("[u]", "<u>", $chaine,$nbU);
	$chaine = str_replace("[/u]","</u>", $chaine,$nbFU); 
	
	// Red
	$nbR=0;
	$nbFR=0;
	$chaine = str_replace("[r]", "<font color='red'>", $chaine, $nbR);
	$chaine = str_replace("[/r]","</font>", $chaine, $nbFR); 

	// yellow
	$nbR=0;
	$nbFR=0;
	$chaine = str_replace("[y]", "<font color='yellow'>", $chaine, $nbR);
	$chaine = str_replace("[/y]","</font>", $chaine, $nbFR); 

	// green
	$nbR=0;
	$nbFR=0;
	$chaine = str_replace("[g]", "<font color='green'>", $chaine, $nbR);
	$chaine = str_replace("[/g]","</font>", $chaine, $nbFR); 

	//Check if the syntaxe is correct
	$chaine = verifClose($chaine,$nbBlod,$nbFBlod,"</strong>");
	$chaine = verifClose($chaine,$nbI,$nbFI,"</i>");
	$chaine = verifClose($chaine,$nbU,$nbFU,"</u>");
	$chaine = verifClose($chaine,$nbU,$nbFU,"</font>");
	


	return $chaine;
}

// this function compare var session with user list to reactualise page if it is necessary 
function verifConnexion()
{
	$fichier = file('../data/User');
	if(isset($fichier))
	{ 
		if(isset($_SESSION['pseudo']))
		{
			if(verifDispoPseudo($_SESSION['pseudo'],$fichier) == false)
			{
				return true;
			}
			return false;
		}

		return false;
	}
	
	return false;
}

function writeMess($mess)
{
	if(verifConnexion())
	{
		 if(isset($mess))
		{

			if($mess)
			{
				$fichier = fopen('../data/Mess', 'a');
				
				if($fichier )
				{

					fseek($fichier, 0);
					$chaine = miseEnForme($mess);
					fputs($fichier,formatTest($_SESSION['pseudo'],$chaine));
					fclose($fichier);

					if($ConnexionList = file('../data/User'))
					{

					$i = recherchePseudo($_SESSION['pseudo'],$ConnexionList);

					if( $i != false)
						{
							// update the users timestamp
							$newUser = $_SESSION['pseudo']." ".$_SERVER['REQUEST_TIME']."\n";
							$ConnexionList[$i] = $newUser;
							file_put_contents("../data/User", implode($ConnexionList));
						}

					}
					$mess = 0;

				}
				else
				{
					echo "erreur de base de donnée";
				}
			}
		}

	}
	else
	{
		// If the user isn't in the user list
		echo "deco";
		session_destroy();

	}
}
?>
